package com.alibaba.algo.apiimpl.feign;

import com.alibaba.algo.cooperation.common.BaseResult;
import com.alibaba.algo.cooperation.feign.vo.req.AddUserGroupReqVO;
import com.alibaba.algo.cooperation.feign.vo.req.SysGroupCreateRequestVO;
import com.alibaba.algo.cooperation.feign.vo.res.EmployeeGroupFeignInfoVO;
import com.alibaba.algo.cooperation.feign.vo.res.UserGroupInfoResVO;
import com.alibaba.algo.cooperation.usercomponent.vo.res.user.EmployeeGroupInfoVO;
import com.alibaba.algo.core.exception.BaseException;
import com.alibaba.algo.domain.user.SysEmployeeGroupDomain;
import com.alibaba.algo.domain.user.SysGroupDomain;
import com.alibaba.algo.entity.user.SysGroup;
import com.alibaba.algo.exception.BaseErrorCode;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author Fan.tianpeng
 * @date 2021/5/8 14:30
 */
@Service
public class UserGroupFeignServiceImpl {

    @Autowired
    private SysGroupDomain sysGroupDomain;

    @Autowired
    private GroupFeignServiceImpl sysGroupService;

    @Autowired
    private SysEmployeeGroupDomain sysEmployeeGroupDomain;

    /**
     * 获取用户组信息列表
     *
     * @return 用户列表
     */
    @Transactional(propagation = Propagation.REQUIRED, readOnly = true, rollbackFor = Exception.class)
    public BaseResult<List<UserGroupInfoResVO>> qryUserGroupList() {
        SysGroup sysGroup = new SysGroup();
        LinkedHashMap<String, Object> sortMap = new LinkedHashMap<>();
        sortMap.put("id", "desc");
        List<SysGroup> sysGroupList = sysGroupDomain.listByPara(sysGroup, sortMap);
        return BaseResult.buildSuccess(covert(sysGroupList));
    }

    private List<UserGroupInfoResVO> covert(List<SysGroup> sysGroupList) {
        List<UserGroupInfoResVO> resVOList = new ArrayList<>();
        sysGroupList.forEach(s -> {
            UserGroupInfoResVO resVO = new UserGroupInfoResVO();
            BeanUtils.copyProperties(s, resVO);
            resVOList.add(resVO);
        });
        return resVOList;
    }

    /**
     * 获取用户组下的员工列表
     *
     * @param groupId 用户组标识
     * @return 员工列表
     */
    @Transactional(propagation = Propagation.REQUIRED, readOnly = true, rollbackFor = Exception.class)
    public BaseResult<List<EmployeeGroupFeignInfoVO>> qryEmployeeListByGroupId(Long groupId) {
        List<EmployeeGroupInfoVO> employeeList = sysEmployeeGroupDomain.selectEmployeeList(groupId);
        if (CollectionUtils.isEmpty(employeeList)) {
            return BaseResult.buildSuccess(Collections.emptyList());
        }
        List<EmployeeGroupFeignInfoVO> result = Collections.synchronizedList(employeeList).parallelStream().map(s -> {
            EmployeeGroupFeignInfoVO feignInfoVO = new EmployeeGroupFeignInfoVO();
            BeanUtils.copyProperties(s, feignInfoVO);
            return feignInfoVO;
        }).collect(Collectors.toList());

        return BaseResult.buildSuccess(result);
    }

    public BaseResult<List<UserGroupInfoResVO>> qryGroupListByEmployeeId(Long employeeId) {
        if (ObjectUtils.isEmpty(employeeId)) {
            return BaseResult.buildSuccess(new ArrayList<>());
        }
        List<SysGroup> groups = sysEmployeeGroupDomain.selectGroupListByEmployeeId(employeeId);
        return BaseResult.buildSuccess(covert(groups));
    }

    /**
     * 新增用户组
     *
     * @return 新增结果
     */
//    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public BaseResult<Long> add(AddUserGroupReqVO vo) {
        if (ObjectUtils.isEmpty(vo.getGroupCode())) {
            throw new BaseException(BaseErrorCode.GROUP_CODE_IS_NULL.getCode(), BaseErrorCode.GROUP_CODE_IS_NULL.getMessage());
        }
        if (ObjectUtils.isEmpty(vo.getGroupName())) {
            throw new BaseException(BaseErrorCode.GROUP_NAME_IS_NULL.getCode(), BaseErrorCode.GROUP_NAME_IS_NULL.getMessage());
        }
        if (CollectionUtils.isEmpty(vo.getEmployeeIdList())) {
            throw new BaseException(BaseErrorCode.EMPLOYEE_ID_LIST_IS_NULL.getCode(), BaseErrorCode.EMPLOYEE_ID_LIST_IS_NULL.getMessage());
        }
        SysGroupCreateRequestVO addVO = new SysGroupCreateRequestVO();
        BeanUtils.copyProperties(vo, addVO);
        Long groupId = sysGroupService.addGroup(addVO);
        return BaseResult.buildSuccess(groupId);
    }

    /**
     * 通过用户组标识列表查询对应的用户组名称映射关系
     *
     * @param vo 用户组标识列表
     * @return 用户组映射关系
     */
    public BaseResult<Map<Long, String>> qryUserGroupNameMap(List<Long> vo) {
        if (CollectionUtils.isEmpty(vo)) {
            return BaseResult.buildSuccess(Collections.emptyMap());
        }
        Map<Long, SysGroup> sysGroupMap = sysGroupDomain.qryUserGroupMapByIdList(vo);
        Map<Long, String> collect = sysGroupMap.entrySet().stream()
                .filter(map -> StringUtils.isNotEmpty(map.getValue().getGroupName()))
                .collect(Collectors.toMap(Map.Entry::getKey, v -> v.getValue().getGroupName(), (v1, v2) -> v2));
        return BaseResult.buildSuccess(collect);
    }
}
